package org.itboys.mobile.service.mongo.web;

import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayActivity;
import org.itboys.mobile.entity.mongo.SundayActivityRelated;
import org.itboys.mobile.entity.mongo.SundayLouKa;
import org.itboys.mobile.entity.mongo.SundayNews;
import org.itboys.mobile.entity.mysql.SundayProduct;
import org.itboys.mobile.service.mysql.web.SundayProductService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/7/18
 * @Description：
 */
@Service
public class SundayActivityRelationService extends BaseMongoService<SundayActivityRelated> {
    @Resource(name = "mobileDs")
    private MongoDataSource dataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return dataSource;
    }
    @Override
    protected Class<SundayActivityRelated> getEntityClass() {
        return SundayActivityRelated.class;
    }

    @Autowired
    private SundayProductService productService;
    @Autowired
    private SundayLouKaService louKaService;
    @Autowired
    private SundayNewsService newsService;
    @Autowired
    private SundayActivityService activityService;

    /**
     * 根据活动id获取相关内容
     * @param request
     * @return
     */
    public PageResult<SundayActivityRelated> select(HttpServletRequest request,Long activityId){
        SundayActivity activity = activityService.getById(activityId);
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request,"activityId");
        param.put("isDeleted", SundayCommonConstants.NO);
        //查询所有的相关，根据相关的类型分类匹配数据
        PageResult<SundayActivityRelated> relatedPageResult = super.pageQuery(request, param);
        List<SundayActivityRelated> data = relatedPageResult.getData();
        if(ListUtil.isNull(data)){return relatedPageResult;}
        List<Long> productIds=new ArrayList<>();//楼盘id集合
        List<Long> louKaIds=new ArrayList<>();//楼卡id集合
        List<Long> newsIDs=new ArrayList<>();//新闻id集合
        for (SundayActivityRelated related:data) {
            if(SundayCommonConstants.activity_related_product.equals(related.getType())){
                productIds.add(related.getOtherId());
            }
            if(SundayCommonConstants.activity_related_louKa.equals(related.getType())){
                louKaIds.add(related.getOtherId());
            }
            if(SundayCommonConstants.activity_related_news.equals(related.getType())){
                newsIDs.add(related.getOtherId());
            }
        }
        //定义三种相关集合
        List<SundayProduct> products=new ArrayList<>();
        List<SundayLouKa> louKas=new ArrayList<>();
        List<SundayNews> newss=new ArrayList<>();
        if(ListUtil.isNotNull(productIds)){
            param.clear();
            param.put("isDeleted",SundayCommonConstants.NO);
            param.put("ids",productIds);
            products = productService.select(param);
        }
        if(ListUtil.isNotNull(louKaIds)){
            param.clear();
            param.put("isDeleted",SundayCommonConstants.NO);
            param.put("id in",louKaIds);
             louKas = louKaService.list(param);
        }
        if(ListUtil.isNotNull(newsIDs)){
            param.clear();
            param.put("isDeleted",SundayCommonConstants.NO);
            param.put("id in",newsIDs);
            newss = newsService.list(param);
        }
        for (SundayActivityRelated related:data) {
            related.setActivityName(activity.getName());
            if(SundayCommonConstants.activity_related_product.equals(related.getType())){
                for (SundayProduct product:products) {
                    if(related.getOtherId().equals(product.getId())){
                        related.setOtherName(product.getName());
                        related.setImage(product.getDefaultImage());
                        break;
                    }
                }
            }else if(SundayCommonConstants.activity_related_louKa.equals(related.getType())){
                for (SundayLouKa louKa:louKas) {
                    if(related.getOtherId().equals(louKa.getId())){
                        related.setOtherName(louKa.getName());
                        related.setImage(louKa.getImage());
                        break;
                    }
                }
            }else if(SundayCommonConstants.activity_related_news.equals(related.getType())){
                for (SundayNews news:newss) {
                    if(related.getOtherId().equals(news.getId())){
                        related.setOtherName(news.getTitle());
                        related.setImage(news.getdefaultImage());
                        break;
                    }
                }
            }
        }
        relatedPageResult.setData(data);
        return  relatedPageResult;
    }

    /**
     * 保存相关
     * @param activityId
     * @param otherId
     * @param type
     */
    public void save(Long activityId,Long otherId,Integer type){

        Map<String,Object> param=new HashMap<>();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("activityId",activityId);
        param.put("otherId",otherId);
        param.put("type",type);
        List<SundayActivityRelated> relateds = super.list(param);
        if(ListUtil.isNotNull(relateds)){
            throw new ErrorException("已存在相关内容");
        }else {
            SundayActivityRelated related=new SundayActivityRelated();
            related.setOtherId(otherId);
            related.setActivityId(activityId);
            related.setType(type);
            super.save(related);
        }
    }
}
